package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Role;
import cn.wolfcode.crm.page.PageResult;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IRoleService;
import cn.wolfcode.crm.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("role")
public class RoleController {


    @Autowired
    private IRoleService roleService;

    @RequestMapping("")
    public String index() {

        return "role";
    }
    @RequestMapping("list")
    @ResponseBody
    public PageResult list(QueryObject qo) {

       PageResult result = roleService.query(qo);

        return result;
    }
    @RequestMapping("save")
    @ResponseBody
    public JsonResult save(Role role) {

        JsonResult result = new JsonResult();
        try{
            roleService.save(role);
            result.setMessage("保存成功");
        }catch (Exception e){
            e.printStackTrace();
            result.mark("保存失败");
        }

        return result;
    }
    @RequestMapping("edit")
    @ResponseBody
    public JsonResult edit(Role role) {

        JsonResult result = new JsonResult();
        try{
            roleService.edit(role);
            result.setMessage("编辑成功");
        }catch (Exception e){
            e.printStackTrace();
            result.mark("编辑失败");
        }

        return result;
    }
    @RequestMapping("listAll")
    @ResponseBody
    public List<Role> listAll() {

        List<Role> list =  roleService.listAll();

        return list;
    }
    @RequestMapping("listIdsByEmployeeId")
    @ResponseBody
    public List<Integer> listIdsByEmployeeId(Long employeeId) {

        List<Integer> ids =  roleService.listIdsByEmployeeId(employeeId);

        return ids;
    }
}